#include "czDisplay.h"
#include "ContextEGL1.h"

#include "InputListenerDefault.h"

#include <GLES/gl.h>
#include "glue.h"

#ifdef _WIN32
#pragma comment (lib, "libgles_cm.lib")
#endif

#include "ilUtil.h"
#include "Font_ext.h"

GLuint tex[1];
ILuint texImg;


int LoadGLTextures()
{
  int status = false;

  ilInit();

  ilGenImages(1, &texImg);
  ilBindImage(texImg);

  ilEnable(IL_ORIGIN_SET);
  ilSetInteger(IL_ORIGIN_MODE, IL_ORIGIN_LOWER_LEFT);
  ilLoad(IL_PNG, "../media/bigfont_i.png");
  ImageInfo info = ImageInfo::Get();

  /* tex x,y layout
  ^ y
  |
  |
  |
  |____________> x
  */
  glGenTextures(1, &tex[0]);
  glBindTexture(GL_TEXTURE_2D, tex[0]);
  glTexImage2D(GL_TEXTURE_2D, 0, info.format, info.w, info.h, 0,
    info.format, info.componentDataType, info.pData);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

  ilDeleteImages(1, &texImg); // can release image data in memory, because submit to VRAM

  status = true;
  return status;
}

void To3DMode(int w, int h)
{
  glViewport(0, 0, w,h);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluePerspectivef(60.0f, (GLfloat)w/(GLfloat)w, 1.0f, 200.0f);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
}

void To2DMode(int w, int h)
{
  glViewport(0,0, w,h);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  //glFrustumf(-(w/2),w/2, -h/2,h/2, 1.0f, 200.0f);
  glOrthof(-(w/2),w/2, -h/2,h/2, 1.0f, 200.0f); // left-upper as (-w/2,h/2)
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
}

void Printf(TextLine& generator, const char* fmt, ...)
{
  static char str[512];

  va_list arg;
  va_start(arg,fmt);
  int len = vsprintf(str,fmt, arg);
  va_end(arg);

  str[len] = str[len+1] = 0;
  generator.ChangeText(str);
  generator.Draw();
}

static int w,h;

int main(int argc, char** argv)
{
  ContextEGL1 ctx;
  ctx.setFlags(CF_RGB|CF_DEPTH);

  MyListener li;
  WindowClass window;
  if( !window.open(&ctx, &li, "title", 640, 480) )
    return -1;

  window.center();
  window.show();

  glEnable(GL_CW);
  glCullFace(GL_BACK);

  w = window.getWidth();
  h = window.getHeight();
  To2DMode(w,h);

  glEnable(GL_TEXTURE_2D);
  LoadGLTextures();

  // Clears the screen in different colours
  glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // clear blue

  // binding constant tex
  TextLine::UseTextureUnit(GL_TEXTURE0, tex[0]);
  TextLine::Init(12,8, 10, 15);
    // '\n', '\t', ' '

  TextLine a[2];
  a[0].Generate(-w/2, h/2, "I want to go! Shall wii done.");
  a[1].Generate(0,0, "MaxPane!", GLFT_GM_CENTER);

  int fps=0;
  while(window.isRun())
  {
    glClear(GL_COLOR_BUFFER_BIT);

    glMatrixMode(GL_MODELVIEW);

    // instance
    glPushMatrix();
    glLoadIdentity();							// Reset The Current Modelview Matrix
    glTranslatef(0,0,-1);
    for(int i=0; i<2; ++i)
      a[i].Draw();
    glPopMatrix();

    Printf(a[1], "out: %d", 45);
    ++fps;

    glFlush();
    window.update();
  }

  TextLine::Shutdown();

  glDeleteTextures(1, tex);

  window.close();

  return 0;
}
